2

ng2 http获取服务器数据的时候传递参数详解

1、 六种方法获取服务器数据所传递的参数

get(url: string, options?: RequestOptionsArgs): Observable<Response>;
post(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>;
put(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>;
delete(url: string, options?: RequestOptionsArgs): Observable<Response>;
patch(url: string, body: string, options?: RequestOptionsArgs): Observable<Response>;
head(url: string, options?: RequestOptionsArgs): Observable<Response>;

2、详解

this.http({
    url: "data/list.json" or "data/list.php" or ......
    method: "get",
    search: "name=zhangxuchao&password=heping" or 使用URLSearchParams() 对象
    headers: 使用Headers()对象设置 可选
    body: ""    可选
})

3、实例

import { Http, Response, URLSearchParams } from 'angular2/http';
let params = new URLSearchParams();
params.set('name', 'huge');
console.log(params.toString()) // name=huge
this.http.get(StaticSettings.BASE_URL, { search: params}).subscribe(
       
);
this.http.post(url, params).subscribe(res=> {
    console.log(res);
});

URLSearchParams说明地址:https://developer.mozilla.org...


张旭超
1.4k 声望222 粉丝

精通 html+div+css jquery, vue, angularjs, angular2, angular4, ionic, ionic2


引用和评论

0 条评论